<?php

namespace App\Http\Controllers;

use App\Http\Requests\FsUserRemarkRequest;
use App\Models\FsUserRemark;
use App\Transformers\FsUserRemarkTransformer;
use App\Transformers\FsUserTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class FsUserRemarkController extends Controller
{
    /**
     * @api {post} /api/fsUser/remark 会员备注添加
     * @apiGroup FsUserRemark
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} remark  备注信息
     * @apiParam {int} fs_user_id  会员id
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 201 OK
     */
    public function store(FsUserRemarkRequest $request,FsUserRemark $fsUserRemark){
        $data = $request->all();
        $data['user_id'] = Auth::id();
        $res = $fsUserRemark->create($data);

        return $this->response->item($res, new FsUserRemarkTransformer('user'));
    }
    /**
     * @api {patch} /api/fsUser/remark/:id 会员备注修改
     * @apiGroup FsUserRemark
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id 备注列表Id(url中传入，必传参数)
     * @apiParam {String} remark  备注信息
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "message": "修改成功"
     *       }
     */
    public function update(FsUserRemarkRequest $request,FsUserRemark $fsUserRemark){
        $this->authorize('own', $fsUserRemark);
        $data = $request->all();
        $fsUserRemark->update($data);
        return $this->response->item($fsUserRemark, new FsUserRemarkTransformer('user'));
    }

    /**
     * @api {post} /api/fsUser/remark/:id 会员备注删除
     * @apiGroup  FsUserRemark
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id 备注列表Id(url中传入，必传参数)
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 204 OK
     */
    public function destroy(FsUserRemark $fsUserRemark){
        $this->authorize('own', $fsUserRemark);
        $fsUserRemark->delete();

        return $this->response->noContent();
    }

    /**
     * @api {get} /api/fsUser/remark 会员备注列表
     * @apiGroup   FsUserRemark
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id 会员Id
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               {
     *                   "id": 2,
     *                   "remark": "bbbbbbbbbb",
     *                   "user_id": 2,
     *                   "fs_user_id": 51,
     *                   "created_at": "2019-01-02 18:07:06",
     *                   "updated_at": "2019-01-02 18:18:23"
     *               }
     *           ]
     *       }
     *
     */
    public function index(Request $request,FsUserRemark $fsUserRemark){
        $validatedData = $request->validate([
            'id' =>"required|exists:mysql_branch.fs_user,id"
        ]);

        $data = $fsUserRemark->where('fs_user_id',$validatedData['id'])->orderBy('created_at','DESC')->get();

        return $this->response->collection($data, new FsUserRemarkTransformer());
    }
    /**
     * @api {get} /api/fsUser/remark/:id?include=user,fsUser 会员备注详情
     * @apiGroup FsUserRemark
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id 备注列表Id(url中传入，必传参数)
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *              {
     *                   "id": 2,
     *                   "remark": "bbbbbbbbbb",
     *                   "user_id": 2,
     *                   "fs_user_id": 51,
     *                   "created_at": "2019-01-02 18:07:06",
     *                   "updated_at": "2019-01-02 18:18:23"
     *              }
     *       }
     *
     */
    public function show(FsUserRemark $fsUserRemark){
        return $this->response->item($fsUserRemark, new FsUserRemarkTransformer());
    }
}
